import router from '@/router'
import store from '@/store'

function checkPermission(el, binding) {
  const { value } = binding
  let obj = {}
  const roles = store.getters && store.getters.roles
  for (let i = 0; i < router.currentRoute.meta.promission.length; i++) {
    if (roles.includes(router.currentRoute.meta.promission[i].key)) {
      obj = router.currentRoute.meta.promission[i]
    }
  }

  if (Object.keys(obj).length > 0) {
    if (!obj.promission.includes(value)) {
      el.parentNode && el.parentNode.removeChild(el)
    }
  }
}

export default {
  inserted(el, binding) {
    checkPermission(el, binding)
  },
  update(el, binding) {
    checkPermission(el, binding)
  }
}
